let db;
let request
// 打开数据库
export const openDB = () => {
    return new Promise((resolve, reject) => {
        var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
        if(!indexedDB) {
            reject()
            throw new Error("你的浏览器不支持IndexedDB");
        }
        request = window.indexedDB.open("FileDatabase", 1);
        // 失败
        request.onerror = (event) => {
            reject(event)
        };
        // 成功
        request.onsuccess = (event) => {
            db = event.target.result;
            resolve(event)
        };
        // 没有数据库则触发新建
        request.onupgradeneeded = (event) => {
            let db = event.target.result;
            if (!db.objectStoreNames.contains("FileTab")) {
                db.createObjectStore("FileTab", { keyPath: "id"});
            } else {
                reject("数据库表名重复")
            }
        }
    })
}
// 新增数据
export const addDb = (obj) => {
    return new Promise((resolve, reject) => {
        let request = db.transaction(["FileTab"], "readwrite").objectStore("FileTab").add(obj);
        // 成功
        request.onsuccess = (event) => { resolve(event) }
        // 失败
        request.onerror = (event) => { reject(event) }
    })
}
// 新增多条
export const addlistDb = (list) => {
    return new Promise((resolve, reject) => {
        let proList = []
        list.forEach(item => {
            proList.push(addDb(item))
        })
        Promise.all(proList).then(res => {
            resolve("add success")
        }).catch(msg => {
            reject(msg)
        })
    })
}
// 查找所有数据
export const findAllDb = () => {
    return new Promise((resolve, reject) => {
        let request = db.transaction(["FileTab"], "readonly").objectStore("FileTab").getAll();
        // 成功
        request.onsuccess = (event) => {
            if (request.result) resolve(request.result) 
            else resolve([])
        };
        // 失败
        request.onerror = (event) => { reject(event) }
    })
}
// 查找单个数据
export const findDb = (id) => {
    return new Promise((resolve, reject) => {
        let request = db.transaction(["FileTab"], "readonly").objectStore("FileTab").get(id)
        // 成功
        request.onsuccess = (event) => {
            if (request.result) resolve(request.result)
            else resolve(null) 
        };
        // 失败
        request.onerror = (event) => { reject(event) };
    })
}
// 删除数据
export const deleteDb = (id) => {
    return new Promise((resolve, reject) => {
        let request = db.transaction(["FileTab"], "readwrite").objectStore("FileTab").delete(id)
        // 成功
        request.onsuccess = (event) => { resolve() }
        // 失败
        request.onerror = (event) => { reject(event) }
    })
}
// 更新数据
export const updateDb = (obj) => {
    return new Promise((resolve, reject) => {
        let request = db.transaction(["FileTab"], "readwrite").objectStore("FileTab").put(obj);
        // 成功
        request.onsuccess = (event) => { resolve() }
        // 失败
        request.onerror = (event) => { reject(event) }
    })
}
// 删除数据表
export const deleteIndexedDBTable = (nameId) => {
    // db.transaction([nameId], "readwrite").objectStore(nameId).clear()
    // v++
    // openDB(nameId, v)
    // return new Promise((resolve, reject) => {
    //     let request = db.transaction([nameId], "readwrite").objectStore(nameId).delete(id);
    //     // 成功
    //     request.onsuccess = (event) => {
    //         resolve()
    //     };
    //     // 失败
    //     request.onerror = (event) => {
    //         reject(event)
    //         console.error("Failed to delete data");
    //     };
    // })
}